﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmManagerMain : Form
    {
        const int NUMBER_DAY_BOXES = 42;
        const int CHECK_IN_INTERVAL = 10000;

        Person user;
        int month;
        int year;
        List<RichTextBox> daysBoxes;
        Form firstForm;
        Timer timer;
        string scheduleText;

        public frmManagerMain(Person User, Form FirstForm)
        {
            firstForm = FirstForm;
            user = User;
            month = DateTime.Now.Month;
            year = DateTime.Now.Year;

            InitializeComponent();

            daysBoxes = new List<RichTextBox>(NUMBER_DAY_BOXES);
            #region addingElementsToList
            daysBoxes.Add(richTextBox1);
            daysBoxes.Add(richTextBox2);
            daysBoxes.Add(richTextBox3);
            daysBoxes.Add(richTextBox4);
            daysBoxes.Add(richTextBox5);
            daysBoxes.Add(richTextBox6);
            daysBoxes.Add(richTextBox7);
            daysBoxes.Add(richTextBox8);
            daysBoxes.Add(richTextBox9);
            daysBoxes.Add(richTextBox10);
            daysBoxes.Add(richTextBox11);
            daysBoxes.Add(richTextBox12);
            daysBoxes.Add(richTextBox13);
            daysBoxes.Add(richTextBox14);
            daysBoxes.Add(richTextBox15);
            daysBoxes.Add(richTextBox16);
            daysBoxes.Add(richTextBox17);
            daysBoxes.Add(richTextBox18);
            daysBoxes.Add(richTextBox19);
            daysBoxes.Add(richTextBox20);
            daysBoxes.Add(richTextBox21);
            daysBoxes.Add(richTextBox22);
            daysBoxes.Add(richTextBox23);
            daysBoxes.Add(richTextBox24);
            daysBoxes.Add(richTextBox25);
            daysBoxes.Add(richTextBox26);
            daysBoxes.Add(richTextBox27);
            daysBoxes.Add(richTextBox28);
            daysBoxes.Add(richTextBox29);
            daysBoxes.Add(richTextBox30);
            daysBoxes.Add(richTextBox31);
            daysBoxes.Add(richTextBox32);
            daysBoxes.Add(richTextBox33);
            daysBoxes.Add(richTextBox34);
            daysBoxes.Add(richTextBox35);
            daysBoxes.Add(richTextBox36);
            daysBoxes.Add(richTextBox37);
            daysBoxes.Add(richTextBox38);
            daysBoxes.Add(richTextBox39);
            daysBoxes.Add(richTextBox40);
            daysBoxes.Add(richTextBox41);
            daysBoxes.Add(richTextBox42);
            #endregion

            refresh();

            timer = new Timer();
            timer.Interval = CHECK_IN_INTERVAL;
            timer.Start();
            timer.Tick += new EventHandler(timer_Tick);

            scheduleText = UTIL.personalizeScheduleText(user, Schedule.loadText());
        }

        void timer_Tick(object sender, EventArgs e)
        {
            user.checkIn();
        }

        void refresh()
        {
            switch (month)
            {
                case 1:
                    lblMonthYear.Text = "January " + Convert.ToString(year);
                    break;
                case 2:
                    lblMonthYear.Text = "February " + Convert.ToString(year);
                    break;
                case 3:
                    lblMonthYear.Text = "March " + Convert.ToString(year);
                    break;
                case 4:
                    lblMonthYear.Text = "April " + Convert.ToString(year);
                    break;
                case 5:
                    lblMonthYear.Text = "May " + Convert.ToString(year);
                    break;
                case 6:
                    lblMonthYear.Text = "June " + Convert.ToString(year);
                    break;
                case 7:
                    lblMonthYear.Text = "July " + Convert.ToString(year);
                    break;
                case 8:
                    lblMonthYear.Text = "August " + Convert.ToString(year);
                    break;
                case 9:
                    lblMonthYear.Text = "September " + Convert.ToString(year);
                    break;
                case 10:
                    lblMonthYear.Text = "October " + Convert.ToString(year);
                    break;
                case 11:
                    lblMonthYear.Text = "November " + Convert.ToString(year);
                    break;
                case 12:
                    lblMonthYear.Text = "December " + Convert.ToString(year);
                    break;
                default:
                    lblMonthYear.Text = Convert.ToString(month) + " " + Convert.ToString(year);
                    break;
            }

            DateTime startDate = new DateTime(year, month, 1);
            DateTime endDate = new DateTime(year, month, 1);
            endDate = endDate.AddDays(DateTime.DaysInMonth(year, month));

            Schedule schedule = new Schedule(startDate, endDate);

            schedule.personalizeSchedule(user);

            int start;
            switch (schedule.Days[0].DateTime.DayOfWeek)
            {
                case System.DayOfWeek.Sunday:
                    start = 0;
                    break;
                case System.DayOfWeek.Monday:
                    start = 1;
                    break;
                case System.DayOfWeek.Tuesday:
                    start = 2;
                    break;
                case System.DayOfWeek.Wednesday:
                    start = 3;
                    break;
                case System.DayOfWeek.Thursday:
                    start = 4;
                    break;
                case System.DayOfWeek.Friday:
                    start = 5;
                    break;
                case System.DayOfWeek.Saturday:
                    start = 6;
                    break;
                default:
                    start = 0;
                    break;
            }

            foreach (RichTextBox item in daysBoxes)
            {
                item.Text = "";
                item.BackColor = Color.White;
            }

            for (int i = 0; i < start; i++)
            {
                daysBoxes[i].BackColor = Color.Gray;
            }

            int j = start;

            while (j < schedule.Days.Count + start)
            {
                GeneralClasses.Day day = schedule.Days[j - start];

                daysBoxes[j].Text = Convert.ToString(j + 1 - start) + ". \n\n";

                List<Shift> shifts = day.getAllShifts();
                for (int i = 0; i < shifts.Count; i++)
                {
                    if (day.DID < 0) //if -1 there is no schedule for that day
                    {
                        daysBoxes[j].Text += "No schedule for day!";
                        daysBoxes[j].BackColor = Color.Red;
                    }
                    else
                    {
                        daysBoxes[j].Text += shifts[j].Name + ": " + Convert.ToString(shifts[j - start].StartTime.TimeOfDay)
                            + " - " + Convert.ToString(shifts[j - start].EndTime.TimeOfDay) + "\n";
                        daysBoxes[j].BackColor = Color.White;
                    }
                }
                j++;
            }

            while (j < NUMBER_DAY_BOXES)
            {
                daysBoxes[j].BackColor = Color.Gray;
                j++;
            }
        }

        private void btnEditSchedules_Click(object sender, EventArgs e)
        {
            frmEditSchedule edit = new frmEditSchedule(this);

            this.Hide();
            edit.Show();
        }

        private void btnCreateSchedule_Click(object sender, EventArgs e)
        {
            frmCreateSchedule create = new frmCreateSchedule(this);

            this.Hide();
            create.Show();
        }

        private void btnManageAccounts_Click(object sender, EventArgs e)
        {
            frmManageAccounts manage = new frmManageAccounts(this);

            this.Hide();
            manage.Show();
        }

        private void btnPreviousMonth_Click(object sender, EventArgs e)
        {
            month--;
            if (month < 1)
            {
                month = 12;
                year--;
            }
            refresh();
        }

        private void btnNextMonth_Click(object sender, EventArgs e)
        {
            month++;
            if (month > 12)
            {
                month = 1;
                year++;
            }
            refresh();
        }

        private void btnAccountSettings_Click(object sender, EventArgs e)
        {
            frmManageAccount settings = new frmManageAccount(this, user);

            this.Hide();
            settings.Show();
        }

        private void btnPreferences_Click(object sender, EventArgs e)
        {
            frmPreferences preferences = new frmPreferences(this, user);

            this.Hide();
            preferences.Show();
        }

        private void btnLogoff_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmManagerMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            firstForm.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            (new frmAllShifts(scheduleText)).Show();
        }
    }
}
